package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductResult;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.utils.UUIDUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service //dubbo
public class ExportServiceImpl implements ExportService {

    @Autowired
    ExportDao exportDao;

    @Autowired
    ContractDao contractDao;

    @Autowired
    ContractProductDao contractProductDao;

    @Autowired
    ExportProductDao exportProductDao;

    @Autowired
    ExtCproductDao extCproductDao;

    @Autowired
    ExtEproductDao extEproductDao;

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(int page, int size, ExportExample example) {
        //设置分页参数
        PageHelper.startPage(page,size);

        //查询所有
        //使用PageInfo包装上面的list
        return new PageInfo(exportDao.selectByExample(example));
    }

    @Override
    public void save(Export export) {
        //A.往export表中新增一条记录
        //1.设置id
        export.setId(UUIDUtils.getId());
        //2.设置合同id,页面上传递过来 contractIds=1,2,3
        //3.通过合同id,查询出所有的合同list
        String[] contractIdArr = export.getContractIds().split(",");
        List<String> contractIdList = Arrays.asList(contractIdArr);
        ContractExample contractExample = new ContractExample();
        ContractExample.Criteria contractExampleCriteria = contractExample.createCriteria();
        contractExampleCriteria.andIdIn(contractIdList);

        List<Contract> contractList = contractDao.selectByExample(contractExample);

        //4.遍历list,获取每个合同
        String cno="";//12 23 34
        if (contractList!=null && contractList.size()>0) {
            for (Contract contract : contractList) {
                //5.拼接合同号
                cno += (contract.getContractNo()+" ");
                //6.给每个每个合同设置状态:2
                contract.setState(2);//已报运
                //7.更新合同信息
                contractDao.updateByPrimaryKeySelective(contract);
            }
        }

        //8.给报运单设置合同号
        export.setCustomerContract(cno);

        //9.给报运单设置create_time和状态
        export.setCreateTime(new Date());
        export.setState(0);//草稿
        ///////////////////////////////////////////////////
        //B.往export_product表中新增n条记录

        //1.查询此报运单涉及到的合同中的所有的货物 list
        ContractProductExample contractProductExample = new ContractProductExample();
        ContractProductExample.Criteria contractProductExampleCriteria = contractProductExample.createCriteria();
        contractProductExampleCriteria.andContractIdIn(contractIdList);

        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);

        //2.遍历货物list,获取到每个货物
        if (contractProductList!=null && contractProductList.size()>0) {
            for (ContractProduct cp : contractProductList) {
                //3.创建报运商品对象
                ExportProduct ep = new ExportProduct();
                //4.将货物中信息设置到报运商品对象中
                //5.为了简单,将货物的id设置给商品id  使用spring中BeanUtils
                BeanUtils.copyProperties(cp,ep);//同名的属性 都会进行拷贝

                //6.给每个商品设置报运单id
                ep.setExportId(export.getId());

                //7.保存每个报运商品
                exportProductDao.insertSelective(ep);
            }
        }
        /////////////ctrl+shift+ 箭头////////////////////////

        //C.往ext_eproduct表中新增n条记录
        //1.查询此报运单涉及到的合同中的所有的附件 list
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractIdIn(contractIdList);

        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

        //2.遍历附件list,获取每个附件
        if (extCproductList!=null && extCproductList.size()>0) {
            for (ExtCproduct ecp : extCproductList) {
                //3.创建报运附件对象
                ExtEproduct eep = new ExtEproduct();

                //4.将附件中的信息设置到报运附件对象中
                //5.为了简单,将附件的id设置给报运附件id  使用spring中BeanUtils
                BeanUtils.copyProperties(ecp,eep);

                //6.给每个报运附件设置商品id(附件的货物id)
                eep.setExportProductId(ecp.getContractProductId());

                //7.给每个报运附件设置报运单id
                eep.setExportId(export.getId());

                //8.保存每个报运附件
                extEproductDao.insertSelective(eep);
            }
        }

        //D.给报运单设置货物和附件数量
        export.setProNum(contractProductList.size());
        export.setExtNum(extCproductList.size());

        //E.保存export
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        //1.更新报运单信息
        exportDao.updateByPrimaryKeySelective(export);

        //2.更新每个报运商品信息
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts!=null && exportProducts.size()>0) {
            for (ExportProduct ep : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(ep);
            }
        }

    }

    @Override
    public void delete(String id) {

    }

    @Override
    //根据报运结果修改报运单和报运商品信息
    public void updateByExportResult(ExportResult exportResult) {
        //1.修改报运单信息
        Export export = new Export();

        //设置id
        export.setId(exportResult.getExportId());
        //修改报运单的状态  "已上报"-->"已报运"
        export.setState(exportResult.getState());
        //修改报运单的备注
        export.setRemark(exportResult.getRemark());

        exportDao.updateByPrimaryKeySelective(export);

        //2.修改报运商品信息
        //获取所有的报运商品list,遍历list
        Set<ExportProductResult> products = exportResult.getProducts();
        if (products!=null && products.size()>0) {
            for (ExportProductResult product : products) {
                //- 给每个报运商品设置关税信息
                ExportProduct exportProduct = new ExportProduct();
                exportProduct.setId(product.getExportProductId());
                exportProduct.setTax(product.getTax());

                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public List<Export> findAll(ExportExample exportExample) {
        return exportDao.selectByExample(exportExample);
    }
}
